from eventServer import eventServer
from carryCube import CarryCube
from vec3 import Vec3
from soundServer import sound

class DeathMace(CarryCube):
    
    def __init__(self):
        CarryCube.__init__(self)
        
        self.setVar("texture", "deathMace")
        self.setVar("scale", Vec3(.8, .8, .8))
        self.setVar("vulnerable", True)
        self.setVar("elasticity", 0.7)
        
        self.killVel = 1
        self.killState = 0
        self.throwPlayer = None
        self.killDelayCount = 0
        self.killDelay = 0.1
        
    def update(self, cmd, args):
        CarryCube.update(self, cmd, args)

        if(self.killState == 0):
            p = self.getVar("itemPlayer")
            if(not(p == None)):
                self.throwPlayer = p
                
                self.killState = 1
        elif(self.killState == 1):
            p = self.getVar("itemPlayer")
            if(p == None):
                self.killDelayCount = self.killDelay
                
                self.killState = 2
        elif(self.killState == 2):
            self.killDelayCount -= args["dtime"]
            if(self.killDelayCount < 0):
                self.setVar("texture", "deathMaceActive")
                sound.play("deathMace_activate")
                
                self.killState = 3
        elif(self.killState == 3):
            v = self.getVar("vel")
            if(v.length() < self.killVel and self.touchingGround()):
                self.setVar("texture", "deathMace")
                sound.play("deathMace_deActivate")
                
                self.killState = 0
        else:
            self.killState = 0
        
    def itemCollision(self, player):
        if(self.killState == 3):
            fromPlayer = self.throwPlayer.getVar("player")
            toPlayer   = player.getVar("player")
            eventServer.pull('playerStomp',{'fromPlayer':fromPlayer,'toPlayer':toPlayer})
            sound.play("deathMace_kill")
        else:
            CarryCube.itemCollision(self, player)